
/// ------------------------------------------------------------------------------------------------ ///
/// ---------------------------  CREATE WINDOWS FOR EVENT AND NON-EVENT  --------------------------- ///
/// ------------------------------------------------------------------------------------------------ ///

*Generate Event Variable
	qui gen Event          = 0
	qui replace Event      = 1 if Inf_Misreport!=. & Date>=d(01feb2007)
	qui gen in_window      = 0
	qui gen out_window     = 0


    *Drop week of national election (Oct 28th)
	*Results are robust if we include these days
	* ------------------------------------------------
	drop if Date>=d(25oct2007) & Date<=d(01nov2007) 

	
	* Create Event Window Type 0: 2-day window
	* ----------------------------------------
	if $window_type <= 0 {
		qui replace in_window  = 1 if   Event==1    | L.Event==1  
		qui replace out_window = 1 if   in_window==0 
	}	
	
	* Create Event Window Type 1: 3-day window
	* -----------------------------------------
	if $window_type == 1{
		qui replace in_window  = 1 if   Event==1    | L.Event==1  | F.Event==1 
		qui replace out_window = 1 if   in_window==0
		qui sum BE_Change if out_window==1, detail
		qui drop if ( BE_Change > r(p99) | BE_Change < r(p1)) & BE_Change!=. & out_window==1
	}	
	
	* Create Event Window Type 2: 2-day window [smaller non-event window]
	* --------------------------------------------------------------------------
	if $window_type == 2 {
		 qui replace in_window  = 1 if   Event==1    | L.Event==1  
		 qui replace out_window = 1 if (in_window==0 & (L2.Event==1 | L3.Event==1 | F3.Event==1 | F2.Event==1))
	}
	
	* Create Event Window Type 3: 3-day window [smaller non-event window]
	* -------------------------------------------------------------------------
	if $window_type == 3 {
		 qui replace in_window  = 1 if   Event==1    | L.Event==1   | F.Event==1 
		 qui replace out_window = 1 if (in_window==0 & (L2.Event==1 | L3.Event==1 | F3.Event==1 | F2.Event==1)) 

	}	
				
*Number of Observations, by Window Type	
	qui sum in_window
	qui scalar Nobs_Tot        = r(N)        // Number of Observations
	qui scalar days_event      = r(sum)      // Number of Event Days	
	qui sum out_window
	qui scalar days_non_event  = r(sum)      // Number of Non-Event Days
	qui gen Event_sum          = Nobs_Tot

*Set again date_id
	qui drop date_id
	qui sort Date
	qui gen date_id = _n
	qui sort date_id
	qui tset date_id
	
*Create the identifier of each window
	qui gen tmp = 0
	qui replace tmp=1 if in_window==1 & L.in_window==0
	qui gen EVENT_ID = sum(tmp)
	qui replace EVENT_ID=0 if in_window==0 
	
*Drop outliers; only for non-event days
if $window_type >=0 {
	local perc      = 0.5
	if $window_type ==2 | $window_type ==3 {
		local perch = 100-`perc'
		local percl = `perc'
	}
	if $window_type ==0 | $window_type ==1 {
		local perch = 100-(`perc'+1)
		local percl = `perc'+1
	}
	gen tmp_var = BE_Change if out_window==1 
	egen pH = pctile(tmp_var),  p(`perch')
	egen pL  = pctile(tmp_var), p(`percl')
	replace BE_Change     = . if BE_Change!=. & (BE_Change>=pH | BE_Change<=pL) & out_window==1
	drop tmp_var pH pL


*Create auxiliary variables [relevant for the SD test]
	qui gen BE_Change_in_window   = BE_Change if in_window==1
	qui gen BE_Change_out_window  = BE_Change if out_window==1	
}
//------------------------------------------------------------------------------
